
var Groups=function(){
	var grid;
	var fs,win;
	var buildGrid = function(){
		grid = new Ext.grid.EditorGridPanel({
			store:new Ext.data.Store({
				proxy:new Ext.data.HttpProxy({url:'./?ctrl=groups&action=get'}),
				reader:new Ext.data.JsonReader({},['id','groupname','intro'])
			}),
			columns:[
			new Ext.grid.RowNumberer({header :'ID',dataIndex:'id'}),
			{header:'组名称',width:200,sortable:true,dataIndex:'groupname',editor:new Ext.form.TextField()},
			{header:'说明',width:550,sortable:true,dataIndex:'intro',editor:new Ext.form.TextField()}
			],
			viewConfig:{
				forceFit:true
			},
			renderTo:'grid',
			height: 500,
			border:false
		});
		grid.on('afteredit',function(e){
			Ext.Ajax.request({
				url:'./?ctrl=groups&action=update',
				params:{
				'id':e.record.data.gid,
				'group[groupname]':e.record.data.groupname,
				'group[intro]':e.record.data.intro
				},
				method:'POST',
				success:function(){
				}
			});
		});
		grid.on('cellclick',function(g,rowIndex,columnIndex,e){
			var url="./?ctrl=groups&action=safeform&id="+g.selModel.selection.record.data.id;
			var f_dom=Ext.get('safe').dom;
			if(f_dom.src!=url){
				f_dom.src=url;
			}
		});
		grid.store.load();
	};
	var buildLayout = function(){
		var win;
		var subfrom;
		var toolbar = new Ext.Panel({
			border:false,
			tbar:[{
				text:'新增',
				cls:'x-btn-text-icon',
				icon:RES_ROOT_PATH+'images/add.gif',
				handler:function(){
					subfrom=new Ext.FormPanel({
						labelWidth: 75, // label settings here cascade unless overridden
						url:'./?ctrl=groups&action=create',
						frame:true,
						//title: '组信息',
						bodyStyle:'padding:0px 0px 0',
						layout:'form',
						width: 350,
						defaults: {width: 230},
						defaultType: 'textfield',
						items: [{
							fieldLabel: '组名字',
							name: 'groupname',
							allowBlank:false
						},{
							fieldLabel: '介绍',
							name: 'intro',
							xtype: 'textarea'
						}
						],
						buttons: [{
							text: '增加',
							handler :function(e){
								subfrom.getForm().submit({
									waitMsg:"提交中",
									failure:function (form,action){Ext.MessageBox.alert("信息","提交失败！");},
									success :function (form,action){
										Ext.MessageBox.alert("信息","提交成功");
										grid.store.reload();
										win.close();
									}
								});
							},
							scope :this
						},{
							text: '取消',
							handler :function(e){win.close();}
						}]
					});
					//subfrom.getForm().
					win=new Ext.Window({
						title: '添加组',
						layout:'fit',
						width:350,
						height:180,
						closeAction:'close',
						plain: true,
						items: [subfrom]
					});
					win.show(this);
				}
			},'-',{
				text:'删除',
				cls:'x-btn-text-icon',
				icon:RES_ROOT_PATH+'images/delete.gif',
				//enableToggle:true,
				//pressed:true,
				handler:function(){
					Ext.Msg.confirm('提示', '是否确认删除？', function(btn){
						if (btn == 'yes'){
							Ext.Ajax.request({
								url: './?ctrl=groups&action=delete',
								failure: function(response , options ){Ext.MessageBox.alert("信息","提交失败！");},
								success: function(response , options ){
									Ext.MessageBox.alert("信息","提交成功！");
									grid.store.reload();
								},
								headers: {
								'my-header': 'foo'
								},
								params: { id: grid.selModel.selection.record.data.id }
							});
						}
					},this);
				}
			}]
		});

		var layout = new Ext.Viewport({
			layout:'border',
			defaults:{border:false},
			items:[{
				region:'north',
				title:'组管理',
				height:52,
				items:toolbar
			},{
				region:'west',
				width:300,
				height: 200,
				split:true,
				autoScroll:true,
				items:grid
			},{
				region:'center',
				layout:'fit',
				title:'权限',
				items:new Ext.Panel({
					renderTo:"panl",
					html : Ext.isIE?'<iframe id="safe" scrolling="auto" frameborder="0" style="width:100%;height:100%;"></iframe>':'<iframe id="safe" scrolling="no" frameborder="0" style="width:100%;height:800px;"></iframe>'
				})
			}]
		});
	}
	return{
		init:function(){
			// Ext.state.Manager.setProvider(new Ext.state.SessionProvider({state: Ext.appState}));
			buildGrid();
			buildLayout();
		}
	}
}();
Ext.onReady(Groups.init,Groups);